
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marwen
 */
public class ListeTrader extends javax.swing.JFrame {

    private Object[][] data;

    public ListeTrader() {
        initComponents();
         this.setLocationRelativeTo(null);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setTitle("Lise des clients ");
                table.setEnabled(false);
                remplirliste();
    }
    public void viderListe(){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int n=model.getRowCount();
        for(int i=0;i<n;i++){
            model.removeRow(n-i-1);
        }
    }
    public void remplirliste(){
        try {
                    viderListe();
                    DefaultTableModel model = (DefaultTableModel) table.getModel();
                    this.setContentPane(jScrollPane1);
                    this.table.setRowHeight(35);
                    DbManip.requete = "SELECT matricule,PRENOM_TRADER, nom_TRADER,poste_tel,DATE_RECRUTEMENT from trader";
                    DbManip.executer(DbManip.requete);
                    int m = DbManip.rsmd.getColumnCount();
                    DbManip.rs.last();
                    int n = DbManip.rs.getRow();
                    int i = 0;
                    DbManip.rs.beforeFirst();
                    data = new Object[n][m];
                    while (DbManip.rs.next()) {
                        int id_trader = DbManip.rs.getInt("matricule");
                        String NOM = DbManip.rs.getString("nom_TRADER").substring(0,DbManip.rs.getString("nom_TRADER").indexOf("   "));
                        String PRENOM = DbManip.rs.getString("PRENOM_TRADER").substring(0,DbManip.rs.getString("PRENOM_TRADER").indexOf("  "));
                        String TEL = DbManip.rs.getString("poste_tel");
                        String date = DbManip.rs.getString("date_recrutement");
        // remplissage du tableau intermédiaire
                        data[i][0] = id_trader;
                        data[i][1] = PRENOM;
                        data[i][2] = NOM;
                        data[i][3] = TEL;
                        data[i][4] = date;            
                        i++;
                    }               
        // Remplissage du tableau d'affichage avec les nouveaux coordonnées            
                for (int l = 0; l < n; l++) {
                //recuperer  grade
                DbManip.requete="select max(ID_GRADE) from TRADER_GRADE where matricule = '"+data[l][0]+"'  group by MATRICULE  ";
                DbManip.executer(DbManip.requete);
                String grade = "";
                while (DbManip.rs.next()) {
                    grade = DbManip.rs.getString("MAX(ID_GRADE)");
                }
                switch (grade) {

            case "1":
                grade = "Junior";
                break;
            case "2":
                grade = "Senior";
                break;
            case "3":
                grade = "Confirmé";
                break;
        }
                model.addRow(new Object[]{data[l][1],data[l][2],grade,data[l][3],data[l][4]});
            }
        }
        catch (SQLException ex) {
            Logger.getLogger(ListeClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Prenom", "Nom", "Grade", "Numéro de poste ", "Date de recrutement"
            }
        ));
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
